function Menu() {
	var menuData;

//	this.addCategoryInMenu = function(galleryId) {
//		var txtMenuData = FullHappyCookie.getCookie('menuData');
//		if (txtMenuData == null || txtMenuData == "") {
//			this.loadMenuData();
//		}else{
//			var menuData = eval('(' + txtMenuData + ')');
//		}
//			
//	};
//	
	this.addNewGalleryToMenu = function(newGalleryId,newGalleryName){
		var txtMenuData = FullHappyCookie.getCookie('menuData');
		menuData = eval('(' + txtMenuData + ')');
		menuData[1].childs.push({"id":newGalleryId,"title":newGalleryName,"link":"","childs":[]});
		FullHappyCookie.setCookie('menuData', JSON.stringify(menuData), 1);
		display('menu');		
	};
	
	this.addNewGalleryToMenu = function(galleryId,newCategoryId,newCategoryName){
		var txtMenuData = FullHappyCookie.getCookie('menuData');
		menuData = eval('(' + txtMenuData + ')');
		menuData[1].childs.push({"id":newGalleryId,"title":newGalleryName,"link":"","childs":[]});
		FullHappyCookie.setCookie('menuData', JSON.stringify(menuData), 1);
		display('menu');		
	};
	
	var appendButtonAddGallery = function() {
		var _li = document.createElement('li');
		var _a = document.createElement('a');

		_a.appendChild(document.createTextNode('Add'));
		_a.setAttribute('class', 'add_gallery');
		_a.setAttribute('href', 'javascript:void(0)');
		//_a.onclick = function (){(new Menu()).openLightBoxAddGallery();};
		_li.appendChild(_a);
		
		$('#mega-menu > li:eq(1) > ul:eq(0)').append(_li);
	};
	
	var appendButtonAddCategory = function() {
		var _li = document.createElement('li');
		var _a = document.createElement('a');

		_a.appendChild(document.createTextNode('Add'));
		_a.setAttribute('class', 'add_category');
		_a.setAttribute('href', 'javascript:void(0)');
		_li.appendChild(_a);
		
		$('#mega-menu > li:eq(1) > ul:eq(0) > li > ul').append(_li);
	};

	var appendButtonEditAndButtonDelete = function() {
		//$('#mega-menu > li:eq(1) ul:eq(0) a').after('<button class="edit"></button>	<button class="delete"></button>');
		$('#mega-menu > li:eq(1) ul:eq(0) a').append('<div class="delete" title="delete"></div> <div class="edit" title="edit"></div>');
		$('#mega-menu > li:eq(1) ul:eq(0) a').css('cursor','default');
	};
	
	
	this.loadMenuData = function() {
		var txtMenuData = FullHappyCookie.getCookie('menuData');
		if (txtMenuData == null || txtMenuData == ""||true) {
			var xmlhttp;
			if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome,
				// Opera,
				// Safari
				xmlhttp = new XMLHttpRequest();
			} else {// code for IE6, IE5
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			xmlhttp.open("GET", "/rest/resources/menu");
			xmlhttp.send();
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
					txtMenuData = xmlhttp.responseText;					
					menuData = eval('(' + txtMenuData + ')');		
					FullHappyCookie.setCookie('menuData', JSON.stringify(menuData), 1);
					display('menu');
					/*
					if (callback && typeof(callback) === "function") {  
				        callback();  
				    }
					*/						
				}
			};
		} else {
			menuData = eval('(' + txtMenuData + ')');					
			display('menu');			
		}
		
	};

	var display = function(contennerId) {

		var contenner = document.getElementById(contennerId);
		if(contenner===null)contenner = window.parent.document.getElementById(contennerId); 
		var menu = createMenu(menuData);
		menu.setAttribute('class', 'mega-menu');
		menu.setAttribute('id', 'mega-menu');
		contenner.appendChild(menu);	
		
		
		if(true){	//isAdmin 	
			appendButtonEditAndButtonDelete();
			appendButtonAddCategory();
			appendButtonAddGallery();
			
			(new AdminActionListener()).clickAddGalleryListener();
			(new AdminActionListener()).clickAddCategoryListener();
			(new AdminActionListener()).clickButtonEditListener();
			(new AdminActionListener()).clickButtonDeleteListener();	
			
			$('#mega-menu').dcMegaMenu({
				rowItems : '5',
				speed : 'fast',
				effect : 'slide'
			});
			
			(new AdminActionListener()).clickCategoryListener();
		}
	};

	var createMenu = function(menuData) {
		var _ul = document.createElement('ul');
		var _li = document.createElement('li');
		var _a = document.createElement('a');
		var l = menuData.length;
		var _label = document.createElement('label');
		for ( var i = 0; i < l; i++) {
			var obj = menuData[i];
			_li = document.createElement('li');
			_a = document.createElement('a');
			_a.innerHTML ='<label>'+obj.title+'</label>';
			if(obj.link!=null && obj.link.length>0){
				_a.setAttribute('href', obj.link);
			}else{
				_a.setAttribute('href', 'javascript:void(0)');
			}
			_li.appendChild(_a);
			_li.setAttribute('_title', obj.title);
			if(obj.id!=null && obj.id.length>0){				
				_li.setAttribute('_id', obj.id);				
			}
			
			if (obj.class != undefined) {
				_a.setAttribute('class', obj.class);
			}
			if (obj.childs != undefined) {
				_li.appendChild(createMenu(obj.childs));
			}
			_ul.appendChild(_li);
		}
		return _ul;
	};
}

